<template>
	<div class="wrapper">
		<!-- header部分 -->
		<header>
			<i class="fa fa-angle-left back-icon" @click="goBack"></i>
			<p>确认订单</p>
		</header>
		<!-- 订单信息部分 -->
		<div class="order-info">
			<h5>订单配送至：</h5>
			<div class="order-info-address" @click="toUserAddress">
				<p>{{ deliveryaddress != null ? deliveryaddress.address : '请选择送货地址' }}</p>
				<i class="fa fa-angle-right"></i>
			</div>
			<!-- <p>{{deliveryaddress!=null?{{{{deliveryaddress.contactName}}{{sexFilter(deliveryaddress.contactSex)}} {{deliveryaddress.contactTel}}}}:':'}}</p> -->
			<p>{{ user.userName }}{{ sexFilter(user.userSex) }} {{ user.userId }}</p>
		</div>
		<h3 v-if="business">{{ business.businessName }}</h3>
		<ul class="order-detailed" v-if="cartArr && cartArr.length > 0">
			<li v-for="item in cartArr" :key="item.food?.foodId">
				<div class="order-detailed-left" v-if="item.food">
					<img :src="item.food.foodImg" alt="food image">
					<p>{{ item.food.foodName }} x {{ item.quantity }}</p>
				</div>
				<p v-if="item.food">&#165;{{ item.food.foodPrice * item.quantity }}</p>
			</li>
		</ul>
		<li v-else>购物车中没有商品</li>



		<div class="order-deliveryfee">
			<p>配送费</p>
			<p>&#165;{{ business.deliveryPrice }}</p>
		</div>
		<!-- 合计部分 -->
		<div class="total">
			<div class="total-left">
				&#165;{{ totalPrice }}
			</div>
			<div class="total-right" @click="toPayment">
				去支付
			</div>
		</div>
	</div>
</template>
<script>
export default {
	/* eslint-disable */
	name: 'Orders',
	data() {
		return {
			businessId: this.$route.query.businessId,
			business: {},
			user: {},
			cartArr: [],  // 确保 cartArr 初始化为空数组
			deliveryaddress: {}
		}
	},
	created() {
		this.user = this.$getSessionStorage('user');

		// 检查是否获取到用户地址，如果没有则初始化为空对象
		this.deliveryaddress = this.$getLocalStorage(this.user.userId) || {};

		//查询当前商家
		let url = "BusinessController/getBusinessById/" + this.businessId;

		this.$axios
			.get(url)
			.then((response) => {
				this.business = response.data.result;

			})
			.catch((error) => {
				console.error(error);
			});

		//查询当前用户在购物车中的当前商家食品列表
		this.$axios.post('CartController/listCart', this.$qs.stringify({
			userId: this.user.userId,
			businessId: this.businessId,
		})).then(response => {
			this.cartArr = response.data.result;
		}).catch(error => {
			console.error(error);
		});
	},
	methods: {
		sexFilter(userSex) {
			if (userSex === 1) {
				return '先生';
			} else if (userSex === 0) {
				return '女士';
			}
			return ''; // 如果没有定义性别，返回空字符串
		},
		toUserAddress() {
			if (!this.user || !this.user.userId) {
				alert('请先登录！');
				return;
			}

			this.$router.push({
				path: '/userAddress',
				query: {
					userId: this.user.userId, // 传递 userId
					businessId: this.businessId // 传递 businessId
				}
			});
		},
		// 返回上一页
		goBack() {
			this.$router.go(-1);
		},
		toPayment() {
			if (!this.deliveryaddress || !this.deliveryaddress.daId) {
				alert('请选择送货地址！');
				return;
			}

			this.$axios.post('OrdersController/createOrders', {
				userId: this.user.userId,
				businessId: this.businessId,
				daId: this.deliveryaddress.daId, // 确保有有效的 daId
				orderTotal: this.totalPrice
			}).then(response => {
				let orderId = response.data;
				if (orderId > 0) {
					this.$router.push({
						path: '/paymentPage',
						query: {
							orderId: orderId
						}
					});
				} else {
					alert('创建订单失败！');
				}
			}).catch(error => {
				console.error(error);
			});

		}
	},
	computed: {
		totalPrice() {
			if (!Array.isArray(this.cartArr) || this.cartArr.length === 0) {
				return 0;  // 如果 cartArr 是空数组或 undefined，返回 0
			}
			let totalPrice = 0;
			for (let cartItem of this.cartArr) {
				if (cartItem.food) {
					totalPrice += cartItem.food.foodPrice * cartItem.quantity;
				}
			}
			totalPrice += this.business.deliveryPrice || 0;
			return totalPrice;
		}
	}
}
</script>
<style scoped>
/****************** 总容器 ******************/
.wrapper {
	width: 100%;
	background-color: #f2f2f2;
}

/****************** header部分 ******************/
.wrapper header {
	width: 100%;
	height: 12vw;
	background-color: deepskyblue;

	position: fixed;
	top: 0;
	left: 0;

	display: flex;
	justify-content: center;
	align-items: center;

	color: white;
	font-size: 5vw;
	font-weight: 700;
}

/****************** 订单信息部分 ******************/
.wrapper .order-info {
	/*注意这里，不设置高，靠内容撑开。因为地址有可能折行*/
	width: 100%;
	box-sizing: border-box;
	padding: 12vw 4vw 4vw;

	background-color: deepskyblue;
	color: white;
	border-bottom-right-radius: 6vw;
	border-bottom-left-radius: 6vw;

	user-select: none;
	cursor: pointer;
}

.wrapper .order-info h5 {
	font-size: 3vw;
	font-weight: 400;
}

.wrapper .order-info .order-info-address {
	width: 100%;
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-weight: 700;
	user-select: none;
	cursor: pointer;
	margin: 1vw 0;
}

.wrapper .order-info .order-info-address p {
	width: 90%;
	font-size: 5vw;
}

.wrapper .order-info .order-info-address i {
	font-size: 6vw;
}

.wrapper .order-info p {
	font-size: 3.5vw;
}

.wrapper h3 {
	box-sizing: border-box;
	padding: 3vw;
	font-size: 4vw;
	color: #666;
	border-bottom: solid 1px #DDD;
}

/****************** 订单明细部分 ******************/
.wrapper .order-detailed {
	width: 100%;
}

.wrapper .order-detailed li {
	width: 100%;
	height: 16vw;
	box-sizing: border-box;
	padding: 3vw;
	margin-top: 2vw;
	margin-bottom: 2vw;

	color: gray;
	font-size: 3.5vw;

	display: flex;
	justify-content: space-between;
	align-items: center;
}

.wrapper .order-detailed li .order-detailed-left {
	display: flex;
	align-items: center;
}

.wrapper .order-detailed li .order-detailed-left img {
	width: 12vw;
	height: 12vw;
	margin-right: 3vw;
}

.wrapper .order-detailed li .order-detailed-left p {
	font-size: 3.5vw;
	margin-left: 3vw;
}

.wrapper .order-detailed li p {
	font-size: 3.5vw;
}

.wrapper .order-deliveryfee {
	width: 100%;
	height: 10vw;
	box-sizing: border-box;
	padding: 3vw;
	color: gray;
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 3.5vw;
}

/****************** 订单合计部分 ******************/
.wrapper .total {
	width: 95%;
	height: 14vw;
	position: fixed;
	bottom: 3vw;
	left: 2.5vw;
	display: flex;
}

.wrapper .total .total-left {
	background-color: dimgray;
	color: white;
	font-size: 4.7vw;
	font-weight: 700;
	border-top-left-radius: 5vw;
	border-bottom-left-radius: 5vw;

	display: flex;
	flex: 2;
	align-items: center;
	justify-content: center;
}

.wrapper .total .total-right {
	background-color: mediumseagreen;
	color: white;
	font-size: 4.7vw;
	font-weight: 700;
	border-top-right-radius: 5vw;
	border-bottom-right-radius: 5vw;

	display: flex;
	flex: 1;
	align-items: center;
	justify-content: center;

	cursor: pointer;
	user-select: none;
}

/* 返回箭头的样式 */
.back-icon {
	font-size: 36px;
	/* 设置图标的大小 */
	position: fixed;
	/* 固定在页面上，不会随内容滚动 */
	top: 13px;
	/* 距离页面顶部 13px */
	left: 20px;
	/* 距离页面左侧 20px */
	color: #1a2edf;
	/* 设置图标的颜色 */
	cursor: pointer;
	/* 鼠标悬停时显示为手型 */
}
</style>